<?php
/**
 * Created by PhpStorm.
 * User: davin.bao
 * Date: 14-4-29
 * Time: 下午4:48
 */

class CultureController extends BaseController {

  protected $culture;
  public function __construct(Info $culture){
    $this->culture = $culture;
  }

    public function getIndex()
    {
        $services = Business::where('freeze','=','0')->orderBy('order', 'DESC')->take(4)->get();
        $setting = Setting::find(1);
        $cultures = Culture::getCompletedList()->where('freeze','=','0')->orderBy('updated_at', 'DESC')->paginate(Config::get('app.pagenate_num'));

        // Show the page
        return View::make(Config::get('app.front_template').'/cultures_index', compact('services','setting','cultures'));
    }

    public function postSearch(){
        $key = Input::get("search");
        $services = Business::where('freeze','=','0')->orderBy('order', 'DESC')->take(4)->get();
        $cultures =Culture::whereRaw("freeze = 0 and (culture_name like '%".$key."%' or culture_content like '%".$key."%')")
           ->orderBy('updated_at', 'DESC')->paginate(Config::get('app.pagenate_num'));
        $setting = Setting::find(1);

        // Show the page
        return View::make(Config::get('app.front_template').'/cultures_index', compact('services','setting','cultures'));
    }

    public function getShow($culture)
    {
        $services = Business::where('freeze','=','0')->orderBy('order', 'DESC')->take(4)->get();
        $setting = Setting::find(1);
        $cultures = Culture::getCompletedList()->where('freeze','=','0')->orderBy('updated_at', 'DESC')->paginate(5);

        // Show the page
        return View::make(Config::get('app.front_template').'/cultures_show', compact('services','setting','cultures','culture'));
    }

  public function getShowAJAX($culture){
    $res = Array('result'=>true,'title'=>$culture->culture_name,'content'=>$culture->culture_content);
    echo json_encode($res);
  }

  public function getMoreAJAX(){
    $pageCount = intval(Config::get("app.pagenate_num"));
    $offset = intval(Input::get( 'offset' ));

    $cultures =   Culture::getCompletedList()->where('freeze','=','0')->orderBy('updated_at', 'DESC')->skip($offset*$pageCount)->take($pageCount)->get();

    $new_cultures = array();
    foreach( $cultures as $culture) {
      $new_cultures[] = array('id' => $culture->id, 'title' => $culture->culture_name, 'created_at' => $culture->created_at, 'content' => String::tidy(Str::limit(strip_tags($culture->culture_content, '<p><a>'), 100)));
    }
    $res = Array('result'=>true,'list'=>$new_cultures);
    echo json_encode($res);
  }
}